package com.top10.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.top10.bean.FileBean;
import com.top10.bean.JwtUtil;
import com.top10.bean.ResponseBean;
import com.top10.common.DatabaseConst;
import com.top10.common.OrderConst;
import com.top10.common.WebConst;

import com.top10.entity.*;
import com.top10.entity.vo.TbCommentVo;
import com.top10.service.IOrderDetailedService;
import com.top10.service.IOrderService;
import com.top10.service.ITbCommentImageService;
import com.top10.service.IUserService;
import com.top10.service.impl.AdminServiceImpl;
import com.top10.service.impl.CommentServiceImpl;
import com.top10.service.impl.TbCommentImageServiceImpl;
import com.top10.utils.GetUuid;
import com.top10.utils.OSSFilePath;
import com.top10.utils.OSSUploadUtils;
import com.top10.utils.OrderDetail;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentServiceImpl commentService;
    @Autowired
    private AdminServiceImpl adminService;
    @Autowired
    private TbCommentImageServiceImpl tbCommentImageService;

    @Autowired
    private FileBean fileBean;

    @Autowired
    private ITbCommentImageService iTbCommentImageService;
    @Autowired
    private IOrderService iOrderService;

    @Autowired
    @Lazy
    private JwtUtil jwtUtil;
    @Autowired
    IUserService userService;

    @Autowired
    private IOrderDetailedService iOrderDetailedService;




    /**
     * 插入评价信息
     * @param comment
     * @return
     * @throws Exception
     */

    @RequestMapping(value = "/manager/getcomment",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean getComment(TbComment comment, HttpServletRequest request,@RequestParam(value = "image",required = false) MultipartFile[] image)throws Exception {


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String uuid= JwtUtil.getUuidByToken(request);
      // String uuid="D26AD7C5C6B6450385A83222A14D58F5";
     /*   String account=adminService.getAccount(uuid);*/


        User user1 = userService.selectOne(new EntityWrapper<User>().eq("uuid", uuid));
        comment.setUuid(new GetUuid().newId());
        comment.setUser_name(user1.getAccount());
        comment.setUser_uuid(uuid);
        comment.setCommentTime(sdf.format(new Date()));
        comment.setIsdeleted(DatabaseConst.IS_DELETED_NO);

        TbCommentImage tbCommentImage=new TbCommentImage();
        tbCommentImage.setCommentUuid(comment.getUuid());


        if(image !=null && image.length> 0){
            for(int i = 0; i< image.length;i++){
              MultipartFile file =image[i];
              String fileName = new GetUuid().newId()+".png";
              String imageSavePath = OSSFilePath.COMMENT + fileName;
              OSSUploadUtils ossUploadUtils = new OSSUploadUtils();
              ossUploadUtils.uploadFile(imageSavePath,file);
              ossUploadUtils.closeOssClient();
              String imagepath= OSSFilePath.OSSDOMAIN  + "/images/file/" +fileName;
              tbCommentImage.setImagepath(imagepath);
              tbCommentImage.setUuid(new GetUuid().newId());
              tbCommentImageService.insertOne(tbCommentImage);
            }
        }


//        Order order=iOrderService.selectOne(new EntityWrapper<Order>().eq("uuid",comment.getOrder_details_uuid()));
//
//        order.setStatus(OrderConst.orderstatus9);
//        iOrderService.updateById(order);


        OrderDetailed orderDetailed=iOrderDetailedService.selectOne(new EntityWrapper<OrderDetailed>().eq("uuid",comment.getOrder_details_uuid()));
        if(orderDetailed==null){
            return ResponseBean.createError("发生错误");
        }
        orderDetailed.setIsdeleted(true);
        iOrderDetailedService.updateById(orderDetailed);

        List<OrderDetailed>  orderDetailed1=iOrderDetailedService.selectList(new EntityWrapper<OrderDetailed>().eq("order_uuid",comment.getOrder_orderid()).eq("isdeleted",0));
        if(orderDetailed1.size()==0){

            Order order=iOrderService.selectOne(new EntityWrapper<Order>().eq("uuid",comment.getOrder_orderid()));
            if(order==null){
                return ResponseBean.createError("发生失败");
            }
            order.setStatus(OrderConst.orderstatus9);
            iOrderService.updateById(order);
        }


        Integer rest=commentService.insert(comment);

        if(rest!=0){
            return ResponseBean.createSuccess().setMessage("成功评价商品");
        }else {
            return ResponseBean.createError("评价商品失败");
        }
    }

 /**
  * 获取商品评价信息
  */
     @RequestMapping(value = "/getall",method = RequestMethod.POST)
     @ResponseBody
     public ResponseBean getAll(TbComment tbComment)throws Exception{

        List<TbCommentVo> tbCommentVoList = commentService.selectAll(tbComment);
        if(tbCommentVoList.size()!=0){
            return ResponseBean.createSuccess().addData("tbCommentVoList",tbCommentVoList);
        }else {
            return ResponseBean.createError("该商品还没被评价");
        }
     }
    /**
     * 获取最新的评论信息
     */
    @RequestMapping(value = "/getcommentcount",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean getCommentCount(String goods_uuid)throws Exception{
         Integer count=commentService.Count(goods_uuid);
         TbCommentVo tbCommentVo=commentService.selectAllOne(goods_uuid);
         if(tbCommentVo ==null){
             return ResponseBean.createSuccess("这件商品没被评价");
         }
         return ResponseBean.createSuccess().addData("tbCommentVo",tbCommentVo).addData("count",count);
    }

    /**
      * 获取有图的商品评价信息
      */
    @RequestMapping(value = "/manager/getimage",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean getimage(String goods_uuid )throws Exception{
      if(goods_uuid ==null || goods_uuid.equals("")){
          return ResponseBean.createError("参数不能为空");
      }
      int i=0;
         List<TbCommentVo> tbCommentVoList=new ArrayList<>();
        List<TbCommentVo>  tbCommentVo1=commentService.selectImage(goods_uuid);
        for(TbCommentVo tbCommentVo : tbCommentVo1){
            List<TbCommentImage> tbCommentVoList1=iTbCommentImageService.selectList(new EntityWrapper<TbCommentImage>().eq("comment_uuid",tbCommentVo.getUuid()));
            if(tbCommentVoList1.size()!=0){
                tbCommentVo.setImagepath(tbCommentVoList1);
                tbCommentVoList.add(tbCommentVo);
                i++;
            }
        }
        if (i>0){
            return ResponseBean.createSuccess().addData("tbCommentVoList",tbCommentVoList);
        }else {
            return ResponseBean.createError("该商品还没有评价图");
        }
    }
}
